<?php
/**
 *  菜单管理
 */
namespace App\Http\Controllers;
use App\Menu;
use Request;

class MenusController extends Controller
{
    public function __construct()
    {
        $this->middleware('check_role');
    }

    public function index(){
        $user = Request::user();
        return view('menus.index',['user' => $user]);
    }

    /**
     * 创建｜更新菜单信息
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author: lxy
     * @time: 2020/6/5 9:33 上午
     * @流程:
     */
    public function save(\Illuminate\Http\Request $request) {
        $server = new \App\Services\MenusService();
        $result = $server->createOrUpdateMenu($request);
        return $this->json($result);
    }

    /**
     * 获取父级菜单
     * @return \Illuminate\Http\JsonResponse
     * @author: lxy
     * @time: 2020/6/5 9:33 上午
     * @流程:
     */
    public function getPrimary() {
        $result = Menu::GetPrimary();
        return $this->json($result);
    }

    /**
     * 获取菜单列表
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author: lxy
     * @time: 2020/6/5 9:34 上午
     * @流程:
     */
    public function getList(\Illuminate\Http\Request $request) {
        $pid = $request->post('pid');
        $result = Menu::GetList($pid);
        return $this->json($result);
    }

    /**
     * 更新菜单状态 启用｜禁用
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author: lxy
     * @time: 2020/6/5 10:49 上午
     * @流程:
     */
    public function updateStatus(\Illuminate\Http\Request $request) {
        $data = $request->post();
        $result = Menu::UpdateStatus($data);
        return $this->json($result);
    }

    /**
     * 删除菜单
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author: lxy
     * @time: 2020/6/5 11:13 上午
     * @流程:
     */
    public function delete(\Illuminate\Http\Request $request) {
        $id = $request->post('id');
        $result = Menu::DelMenus($id);
        return $this->json($result);
    }

    /**
     * 菜单树形结构
     * @return \Illuminate\Http\JsonResponse
     * @author: lxy
     * @time: 2020/6/5 2:35 下午
     * @流程:
     */
    public function menusTree() {
        $result = Menu::MenusTree();
        return $this->json($result);
    }

    protected function json($data, $status = 200) {
        return response()->json($data, $status, [], JSON_UNESCAPED_UNICODE);
    }
}